//package com.xspace.android.xafview.activity;
//
//import android.content.ContentResolver;
//import android.content.Intent;
//import android.graphics.Bitmap;
//import android.graphics.BitmapFactory;
//import android.graphics.Matrix;
//import android.net.Uri;
//import android.os.Bundle;
//import android.provider.MediaStore;
//import android.text.TextUtils;
//import android.util.Log;
//import android.view.inputmethod.InputMethodManager;
//
//import com.xspace.android.imcommon.utils.AppUtil;
//import com.xspace.android.xafcommon.base.BaseActivity;
//import com.xspace.android.xafcommon.base.ImageConfirmAct;
//import com.xspace.android.xafcommon.util.CompressImage;
//import com.xspace.android.xafcommon.util.LogUtils;
//
//import java.io.ByteArrayInputStream;
//import java.io.ByteArrayOutputStream;
//import java.io.File;
//import java.io.IOException;
//import java.io.InputStream;
//
//public abstract class CameraActivity extends BaseActivity {
//	private final int NONE = 0;
//	public final int PHOTOHRAPH = 1;
//	public final int PHOTOCHOICE = 2;
//	public final int PHOTORESOULT = 3;
//	public final int COMFIRMRESOUT = 4;
//	private final String IMAGE_UNSPECIFIED = "image/*";
//	private boolean zoomFlag = false;
//	private int compression = 80;
//	private int outputX = 64;
//	private int outputY = 64;
//	private int pgSize = 25;
//	private Bitmap photo;
//	private Uri imgUri;
//	private byte[] photoByte;
//	private String filePath;
//	private String mobileType = "";
//	private boolean needSave = false;
//	/**是否工作记录 用于图片压缩**/
//	private boolean isAddWorkLog;
//	private int photo_image_flag = -1;
//
//	@Override
//	protected void onCreate(Bundle savedInstanceState) {
//		super.onCreate(savedInstanceState);
//		initialize();
//		mobileType = android.os.Build.MANUFACTURER;
//		LogUtils.d("mobileType===>" + mobileType);
//		AppUtil.createDirs();
//	}
//
//	public abstract void initialize();
//
//	@Override
//	protected void onSaveInstanceState(Bundle outState) {
//		outState.putString("filePath", filePath);
//		outState.putBoolean("zoomFlag", zoomFlag);
//		outState.putInt("photo_image_flag", photo_image_flag);
//		super.onSaveInstanceState(outState);
//	}
//
//	@Override
//	protected void onRestoreInstanceState(Bundle savedInstanceState) {
//		super.onRestoreInstanceState(savedInstanceState);
//		if (TextUtils.isEmpty(filePath)) {
//			filePath = savedInstanceState.getString("filePath");
//			zoomFlag = savedInstanceState.getBoolean("zoomFlag");
//			photo_image_flag = savedInstanceState.getInt("photo_image_flag");
//		}
//
//	}
//
//	protected void onDestroy() {
//		super.onDestroy();
//		if (photo != null)
//			photo.recycle();
//	}
//
//	public void hideSoftInput() {
//		CameraActivity.this.runOnUiThread(new Runnable() {
//			@Override
//			public void run() {
//				((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
//						CameraActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
//			}
//
//		});
//	}
//
//	public void choiceCmaera(String path) {
//		this.filePath = path;
//		photo_image_flag = 0;
//		Intent intent = new Intent(Intent.ACTION_PICK, null);
//		intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED);
//		startActivityForResult(intent, PHOTOCHOICE);
//	}
//
//	public void startCmaera(String path) {
//		this.filePath = path;
//		photo_image_flag = 1;
//		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//		intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(this.filePath)));
//		startActivityForResult(intent, PHOTOHRAPH);
//	}
//	//	public void startCmaera(String path,boolean isNeedWaterMark) {
//	//		this.filePath = path;
//	//		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//	//		intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(this.filePath)));
//	//		startActivityForResult(intent, PHOTOHRAPH);
//	//	}
//
//	public void setPhotoZoom(boolean zoomFlag, int compression, int outputX, int outputY) {
//		this.zoomFlag = zoomFlag;
//		this.compression = compression;
//		this.outputX = outputX;
//		this.outputY = outputY;
//	}
//
//	public void setPhotoZoom(boolean zoomFlag) {
//		this.zoomFlag = zoomFlag;
//	}
//
//	public void setSave(boolean needSave) {
//		this.needSave = needSave;
//	}
//
//	/**是否添加日志**/
//	public void setAddWorkLog(boolean isAddWorkLog) {
//		this.isAddWorkLog = isAddWorkLog;
//	}
//
//	@Override
//	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//
//		if (resultCode == NONE || requestCode == 120)
//			return;
//		if (requestCode == COMFIRMRESOUT) {
//			Log.i("chat", "camera Act   00000>>>>>>>>>");
//			savePictrue(filePath);
//		} else {
//			if (requestCode == PHOTOHRAPH) {
//				//拍照
//				if ("Meizu".equalsIgnoreCase(mobileType) || "samsung".equalsIgnoreCase(mobileType)) {
//					rotatePathMap(filePath);
//				}
//				File picture = new File(filePath);
//				imgUri = Uri.fromFile(picture);
//				Log.i("chat", "camera Act   11111111>>>>>>>>>");
//			}
//			if (requestCode == PHOTOCHOICE) {
//				//相册
//				Log.e("chat", "camera Act   222222>>>>>>>>>data:" + data);
//				if (data == null)
//					return;
//				imgUri = data.getData();
//				Log.e("chat", "camera Act   666666>>>>>>>>>imgUri:" + imgUri);
//			}
//			if (zoomFlag) {
//				Log.e("chat", "camera Act   3333333>>>>>>>>>");
//				if (requestCode == PHOTORESOULT) {
//					if (data == null)
//						return;
//					Bundle extras = data.getExtras();
//					if (extras != null) {
//						photo = extras.getParcelable("data");
//						ByteArrayOutputStream stream = new ByteArrayOutputStream();
//						photo.compress(Bitmap.CompressFormat.JPEG, compression, stream);
//						photoByte = stream.toByteArray();
//						try {
//							stream.close();
//						} catch (IOException e) {
//						}
//						cemareResult(photo, photoByte);
//					}
//					return;
//				}
//				startPhotoZoom(imgUri);
//			} else {
//				Log.i("chat", "camera Act   444444444>>>>>>>>>");
//				ContentResolver resolver = getContentResolver();
//				try {
//					if (needSave) {
//						Log.i("chat", "camera Act   5555555>>>>>>>>>");
//						pictureConfirm();
//					}
//
//					photoByte = readStream(resolver.openInputStream(imgUri));
//					Log.i("chat", "camera Act   ooooooo>>>>>>>>>" + photoByte);
//					scalePicture(300, 520);
//				} catch (Exception e) {
//				}
//			}
//		}
//
//		super.onActivityResult(requestCode, resultCode, data);
//	}
//
//	public abstract void cemareResult(Bitmap photo, byte[] photoByte);
//
//	public abstract void savePictrue(String imagePath);
//
//	public void startPhotoZoom(Uri uri) {
//		Intent intent = new Intent("com.android.camera.action.CROP");
//		intent.setDataAndType(uri, IMAGE_UNSPECIFIED);
//		intent.putExtra("crop", "true");
//		intent.putExtra("aspectX", 1);
//		intent.putExtra("aspectY", 1);
//		intent.putExtra("outputX", outputX);
//		intent.putExtra("outputY", outputY);
//		intent.putExtra("noFaceDetection", true);
//		intent.putExtra("return-data", true);
//		startActivityForResult(intent, PHOTORESOULT);
//	}
//
//	private void scalePicture(int maxWidth, int maxHeight) {
//		try {
//			ByteArrayOutputStream stream = new ByteArrayOutputStream();
//			ByteArrayInputStream isBm = new ByteArrayInputStream(photoByte);
//			BitmapFactory.Options newOpts = new BitmapFactory.Options();
//			newOpts.inJustDecodeBounds = true;
//			newOpts.inTempStorage = new byte[1024 * 1024 * 8];
//			photo = BitmapFactory.decodeStream(isBm, null, newOpts);
//			int srcWidth = newOpts.outWidth;
//			int srcHeight = newOpts.outHeight;
//			int desWidth = 0;
//			int desHeight = 0;
//			double ratio = 0.0;
//			if (srcWidth > srcHeight) {
//				ratio = srcWidth / maxWidth;
//				desWidth = maxWidth;
//				desHeight = (int) (srcHeight / ratio);
//			} else {
//				ratio = srcHeight / maxHeight;
//				desHeight = maxHeight;
//				desWidth = (int) (srcWidth / ratio);
//			}
//			compression = 100 - (photoByte.length / 1024 / pgSize);
//			if (compression < 40) {
//				compression = 40;
//			}
//			newOpts.inSampleSize = (int) (ratio) + 1;
//			newOpts.inJustDecodeBounds = false;
//			newOpts.outWidth = desWidth;
//			newOpts.outHeight = desHeight;
//			isBm = new ByteArrayInputStream(photoByte);
//			photo = BitmapFactory.decodeStream(isBm, null, newOpts);
//			//			photo = PhotoHelper.createWatermark(CameraActivity.this, photo, "微农信");
//			photo.compress(Bitmap.CompressFormat.JPEG, compression, stream);
//			photoByte = stream.toByteArray();
//			Log.v("CameraActivity", "photo set >>>");
//			newOpts.inTempStorage = new byte[0];
//			stream.close();
//			isBm.close();
//			// if ("samsung".equalsIgnoreCase(mobileType)) {
//			// photo = rotateBitMap(photo);
//			// }
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		cemareResult(photo, photoByte);
//	}
//
//	private synchronized void rotatePathMap(String path) {
//		Bitmap bp = null;
//		try {
//			Matrix matrix = new Matrix();
//			matrix.reset();
//			matrix.postRotate(90);
//
//			bp = CompressImage.getSmallBitmap(path, CameraActivity.this);
//			if (bp.isRecycled()) {
//				bp.recycle();
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
//
//	private Bitmap rotateBitMap(Bitmap bp) {
//		Matrix matrix = new Matrix();
//		matrix.reset();
//		matrix.postRotate(90);
//		Bitmap nowBp = Bitmap.createBitmap(bp, 0, 0, bp.getWidth(), bp.getHeight(), matrix, true);
//		if (bp.isRecycled()) {
//			bp.recycle();
//		}
//		return nowBp;
//	}
//
//	private byte[] readStream(InputStream inStream) {
//		byte[] buffer = new byte[1024];
//		int len = -1;
//		byte[] data = null;
//		try {
//			ByteArrayOutputStream outStream = new ByteArrayOutputStream();
//			while ((len = inStream.read(buffer)) != -1) {
//				outStream.write(buffer, 0, len);
//			}
//			data = outStream.toByteArray();
//			outStream.close();
//			inStream.close();
//		} catch (Exception e) {
//		}
//		return data;
//
//	}
//
//	public void pictureConfirm() {
//
//		Intent intentConfirm = new Intent();
//		intentConfirm.setClass(this, ImageConfirmAct.class);
//		intentConfirm.setData(imgUri);
//		intentConfirm.putExtra("imageUrl", filePath);
//		intentConfirm.putExtra("photo||image", photo_image_flag);
//		intentConfirm.putExtra("isAddWorkLog", isAddWorkLog);
//		Log.v("cameraAct", "pictureConfirm >>>> isAddWorkLog:" + isAddWorkLog);
//		startActivityForResult(intentConfirm, COMFIRMRESOUT);
//	}
//}
